<template>
  <div class="data-file">
    <div class="file-view">
      <div class="file-tip">点击选择文件或拖拽文件至此</div>
      <el-upload action="/" class="file-uploader"
        :auto-upload="false"
        :drag="true"
        :on-remove="handleRemove"
        :limit="2"
        :on-change="handleChange"
        :file-list="fileList">
        <i class="el-icon-plus file-uploader-icon"></i>
      </el-upload>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      default () {
        return {}
      }
    }
  },

  data () {
    return {
      fileList: []
    }
  },

  watch: {
    value: {
      deep: true,
      handler () {
        this.refresh()
      }
    }
  },

  mounted () {
    this.refresh()
  },

  methods: {
    refresh () {
      if (this.value.src) {
        this.fileList = [
          { name: this.value.src, raw: this.value.raw}
        ]
      } else {
        this.fileList = []
      }
    },
    handleChange(file) {
      this.fileList.pop()
      this.fileList.push(file)
      this.$emit('input', { src: file.name, raw: file.raw })
    },
    handleRemove() {
      this.$emit('input', {})
    }
  }
}
</script>

<style lang="less" scoped>
.data-file {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 8px 0;
  .file-view {
    max-width: 280px;
  }
  .file-tip {
    color: #999;
    font-size: 12px;
    font-style: italic;
    text-align: center;
  }
}
.file-uploader {
  /deep/.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  /deep/.el-upload:hover {
    border-color: #409EFF;
  }
  /deep/.el-upload-dragger {
    width: 280px;
  }
}
.file-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
</style>